package com.example.pei.ithinklearnt;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.example.pei.ithinklearnt.bean.UserInfo;

import com.example.pei.ithinklearnt.utils.BeanOperationUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


import cn.bmob.newim.BmobIM;

import static com.example.pei.ithinklearnt.Config.DEBUG;

/**
 *
 */

public class AppApplication extends MultiDexApplication {

    private static Context mContext;
    private static UserInfo currentUser;    //当前登陆的de的用户

    private static AHBottomNavigation bottomNavigation;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        /**
         * 初始化image-loader
         */
        initImageLoader();

        /**
         * 初始化bmob sdk
         */
        if (getApplicationInfo().packageName.equals(getMyProcessName())) {
            BmobIM.init(this);
            BmobIM.registerDefaultMessageHandler(new BmobMessageHandler(this));
        }

        /**
         * 初始化bugly sdk
         */
        CrashReport. initCrashReport(mContext , Config.BUGLY_APP_ID ,DEBUG);


    }

    private void initImageLoader() {
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
    }

    /**
     * 获取当前运行的进程名
     *
     * @return
     */
    public static String getMyProcessName() {
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            String processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
            return processName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void setAHBottomNavigation(AHBottomNavigation a){bottomNavigation=a;}
    public static AHBottomNavigation getBottomNavigation(){return bottomNavigation;}
    public static Context getContext(){
        return mContext;
    }
    public static void setCurrentUser(UserInfo u){
        currentUser=u;
        BeanOperationUtil.putBean(mContext,"currentUser","1",currentUser);
    }
    public static UserInfo getCurrentUser(){
        if(currentUser==null){   //防止长时间后台currentUser被回收变null ，应该是这样
            Object o=BeanOperationUtil.getBean(mContext,"currentUser","1");
            if(o instanceof UserInfo){
                currentUser=(UserInfo)o;
            }
        }
        return currentUser;
    }
}
